﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.Office.Server.ApplicationRegistry.Infrastructure;
using Microsoft.Office.Server.ApplicationRegistry.MetadataModel;
using Microsoft.Office.Server.ApplicationRegistry.SystemSpecific.Db;


public partial class WebPartUserControls_UpdateProduct : System.Web.UI.UserControl
{
    private string sSPName = "SharedServices1";
    [System.ComponentModel.Browsable(true)]
    [System.ComponentModel.Category("BDC")]
    public string SSPName
    {
        get { return sSPName; }
        set { sSPName = value; }
    }

    private string lobSystemInstance = "AdventureWorksInstance";
    [System.ComponentModel.Browsable(true)]
    [System.ComponentModel.Category("BDC")]
    public string LobSystemInstance
    {
        get { return lobSystemInstance; }
        set { lobSystemInstance = value; }
    }

    private string entityName = "Production.Product";
    [System.ComponentModel.Browsable(true)]
    [System.ComponentModel.Category("BDC")]
    public string EntityName
    {
        get { return entityName; }
        set { entityName = value; }
    }

    private NamedLobSystemInstanceDictionary lobInstances;
    private LobSystemInstance lobInstance;
    private Entity entity;

    protected void Page_Load(object sender, EventArgs e)
    {
        //find entity instance
        if (SSPName != null)
            SqlSessionProvider.Instance().SetSharedResourceProviderToUse(SSPName);
        lobInstances = ApplicationRegistry.GetLobSystemInstances();
        lobInstance = lobInstances[LobSystemInstance];
        entity = lobInstance.GetEntities()[EntityName];

        if (!IsPostBack)
            this.LoadProduct();

    }

    private void LoadProduct()
    {
        if (Context.Request.QueryString["id"] == null) return;

        object[] parameters = new object[1];
        parameters[0] = System.Int32.Parse(Context.Request.QueryString["id"]);   

        DbEntityInstance record = (DbEntityInstance)entity.FindSpecific(parameters, lobInstance);

        //populate the form 
        this.ProductIDValueLabel.Text = record["ProductID"].ToString();
        this.SafetyStockLevelTextBox.Text = record["SafetyStockLevel"].ToString();
        this.ReorderPointTextBox.Text = record["ReorderPoint"].ToString();
        this.StandardCostTextBox.Text = record["StandardCost"].ToString();
        this.ListPriceTextBox.Text = record["ListPrice"].ToString();
       
    }


    protected void SubmitLinkButton_Click(object sender, EventArgs e)
    {
        object[] parameters = new object[5];
        parameters[0] = Int32.Parse(this.ProductIDValueLabel.Text);
        parameters[1] = Int16.Parse(this.ReorderPointTextBox.Text);
        parameters[2] = Int16.Parse(this.SafetyStockLevelTextBox.Text);
        parameters[3] = System.Decimal.Parse(this.StandardCostTextBox.Text);
        parameters[4] = System.Decimal.Parse(this.ListPriceTextBox.Text);


        //find entity instance
        if (SSPName != null)
            SqlSessionProvider.Instance().SetSharedResourceProviderToUse(SSPName);
        lobInstances = ApplicationRegistry.GetLobSystemInstances();
        lobInstance = lobInstances[LobSystemInstance];
        entity = lobInstance.GetEntities()[EntityName];

        MethodInstance methInst = entity.GetMethodInstances()["ProductUpdater"];
        entity.Execute(methInst, lobInstance, ref parameters);

    }
}
